<template>
  <el-dialog
    title="指令下发"
    :visible.sync="visible"
    width="800px"
    :before-close="handleClose"
  >
    <el-form
      size="small"
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
    >
      <el-form-item label="设备号">
        {{ this.deviceId }}
      </el-form-item>
      <el-form-item label="端口号">
        <el-select v-model="formInline.socketNum">
          <el-option label="1" :value="1"></el-option>
          <el-option label="2" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="指令类型">
        <el-select v-model="formInline.cmdCode" @change="handleChange">
          <el-option
            v-for="item in cmdOptions"
            :key="item.cmdCode"
            :label="item.name"
            :value="item.cmdCode"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div>
      <el-input
        type="textarea"
        :rows="20"
        placeholder="请输入内容"
        v-model="cmdParams"
      >
      </el-input>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button size="small" type="primary" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { cmdList, sendCmd } from '@/api/device'
export default {
  props: {},
  mixins: [],
  components: {},
  data() {
    return {
      visible: false,
      formInline: {
        socketNum: '',
        cmdCode: '',
      },
      deviceId: '',
      cmdOptions: [],
      socketOptions: [],
      cmdParams: '',
      cmdObj: '',
    }
  },
  computed: {
    // Your computed here
  },
  watch: {
    // Your watch here
  },
  methods: {
    getCmdList() {
      cmdList({
        deviceId: this.deviceId,
        pageNo: 1,
        pageSize: 999,
      }).then((res) => {
        console.log(res)
        this.cmdOptions = res
      })
    },
    handleOpen(row) {
      console.log(row)
      this.visible = true
      this.deviceId = row.deviceId
      this.getCmdList()
    },
    handleClose(done) {
      console.log(done)
      this.handleReset()
    },
    handleReset() {
      this.formInline = {
        socketNum: '',
        cmdCode: '',
      }
      this.cmdParams = ''
      this.cmdObj = ''
      this.visible = false
    },
    handleChange(val) {
      console.log(val)
      this.cmdObj = this.cmdOptions.find((option) => option.cmdCode === val)
      const tempParams = {
        method: this.cmdObj.method,
        params: this.cmdObj.params,
      }
      this.cmdParams = JSON.stringify(tempParams, null, 2)
    },
    onSubmit() {
      const cmd = JSON.parse(this.cmdParams)
      sendCmd({
        ...this.cmdObj,
        ...cmd,
        socketNum: this.formInline.socketNum,
      }).then((res) => {
        console.log(res)
        this.$message({
          message: '发送成功',
          type: 'success',
        })
        this.handleClose()
      })
    },
  },
  created() {
    // console.log('Component created');
  },
  mounted() {
    // console.log('Component created');
  },
}
</script>

<style scoped>
/* Your styles here */
</style>
